﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using BLL;

public partial class MembershipCard_wechat_web_Address : WeiPage
{
    PRODUCTBO pbo = new PRODUCTBO();
    ShoppingCartBO sbo = new ShoppingCartBO();
    PersonInfoBO fbo = new PersonInfoBO();
    AddressBO abo = new AddressBO();
    public string g = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                try
                {
                    string url = string.Format(@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + GetAppid() + "&secret=" + GetSecret() + "&code={0}&grant_type=authorization_code", Request.QueryString["code"].ToString());
                    string token = PostRequest(url);
                    if (token.Contains("7200"))
                    {
                        string[] b = token.Split('\"');
                        Session["FromUserName"] = b[13];
                        Session["ToUserName"] = "wx8ecea9aaa3d6162b";
                    }
                }
                catch (Exception)
                {

                }
            }
            if ((Request.QueryString["FromUserName"] != null || Session["FromUserName"] != null) && (Request.QueryString["ToUserName"] != null || Session["ToUserName"] != null))
            {
                string user = Request.QueryString["FromUserName"] == null ? Session["FromUserName"].ToString() : Request.QueryString["FromUserName"].ToString();
                string user2 = Request.QueryString["ToUserName"] == null ? Session["ToUserName"].ToString() : Request.QueryString["ToUserName"].ToString();
                Session["FromUserName"] = user;
                Session["ToUserName"] = user2;

                if (Request.QueryString["did"] != null)
                {
                    abo.Delete(int.Parse(Request.QueryString["did"].ToString()));
                }

                PersonInfo per = fbo.GetModel(user);
                if (per != null)
                {

                    List<ShoppingCart> list = sbo.GetModelList(" pid=" + per.ID);
                    if (list.Count > 0)
                    {
                        sumcar.InnerHtml = "<span><a href='ShopCar.aspx'><em class='indexicon4'></em><i>" + list.Count + "</i></a></span>";
                    }

                    List<Addresss> listadd = abo.GetModelList(" pid=" + per.ID);

                    addlist.InnerHtml = "<div class='btn' style='margin-bottom:20px;'  onclick='add()'>新增收货地址</div>";
                    foreach (Addresss a in listadd)
                    {
                        addlist.InnerHtml += string.Format(@" <div class='order_address_cao' >
    <p onclick='settime({3})'>{0}：{1}<br />
    {2}</p>
    <div class='order_address_btn'>
      <ul>
         <li><a href='#' onclick='up({3})'><span class='order_editor'>修改</span></a></li>
         <li><a href='#' onclick='return del({3})'><span class='order_delete'>删除</span></a></li>
      </ul>
    </div>
  </div>", a.Name, a.Phone,  a.Address, a.ID);
                    }
                    if (Request.QueryString["g"] != null&&Request.QueryString["g"].ToString()=="0")  //从个人中心进入，不加返回订单
                    addlist.InnerHtml += "<div class='btn' style='margin-bottom:20px;'  onclick='dd()'>返回订单</div>";
                    if (Request.QueryString["g"] != null)
                        g = Request.QueryString["g"].ToString();
                }
            }
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        if (Session["orderlx"] != null)
        {
            if (Session["orderlx"].ToString() == "jx")
            {
                if (Session["orderid"] != null && Session["ordernum"] != null)
                {
                    Response.Redirect("Order.aspx?id=" + Session["orderid"].ToString() + "&num=" + Session["ordernum"].ToString());
                }
            }
            else if (Session["orderlx"].ToString() == "gwc")
            {
                if (Session["ids"] != null)
                {
                    Response.Redirect("Order.aspx?ids=" + Session["ids"].ToString());
                }
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["g"] != null&&Request.QueryString["g"].ToString()=="0")  //不是从个人中心过来
        {
            try
            {

                Addresss a = abo.GetModel(int.Parse(aid.Value));
                if (a != null)
                {
                    a.LAST_DATE = DateTime.Now;
                    abo.Update(a);
                }
                if (Session["orderlx"] != null)
                {
                    if (Session["orderlx"].ToString() == "jx")
                    {
                        if (Session["orderid"] != null && Session["ordernum"] != null)
                        {
                            Response.Redirect("Order.aspx?id=" + Session["orderid"].ToString() + "&num=" + Session["ordernum"].ToString());
                        }
                    }
                    else if (Session["orderlx"].ToString() == "gwc")
                    {
                        if (Session["ids"] != null)
                        {
                            Response.Redirect("Order.aspx?ids=" + Session["ids"].ToString());
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }
    }
}